package Demo12;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        //当前磁头所在的柱面号
        int nownum = s.nextInt();
        int len = s.nextInt();
        Integer[] list = new Integer[len];
        for (int i = 0; i < list.length; i++) {
            list[i] = s.nextInt();
        }
        //-1或1
        //flag标志
        int flag = s.nextInt();
        //冒泡排序两种情况
        if (flag == -1) {
            //升序排序
            Arrays.sort(list);
        } else if (flag == 1) {
            //降序排序
            Arrays.sort(list, new Comparator<Integer>() {
                @Override
                public int compare(Integer a, Integer b) {
                    return b-a;
                }
            });
        }
        //两种情况
        if (flag == 1) {
            //当为1时，表示移动臂向柱面号增大方向移动
            int pos = return_pos(nownum, list, flag);
            list = return_list(pos, list);
        } else if (flag == -1) {
            //当为-1时，表示移动臂向柱面号减小的方向的移动
            int pos = return_pos(nownum, list, flag);
            list = return_list(pos, list);
        }
        for (int j : list) {
            System.out.print(j + " ");
        }
    }

    public static int return_pos(int num, Integer[] list, int flag) {
        int pos = 0;
        for (int i = 0; i < list.length; i++) {
            if (flag == -1) {
                if (num < list[i]) {
                    pos = i;
                    break;
                }
            } else if (flag == 1) {
                if (num > list[i]) {
                    pos = i;
                    break;
                }
            }
        }
        return pos;
    }

    public static Integer[] return_list(int pos, Integer[] list) {
        int k = pos - 1;
        for (int j = 0; j < pos / 2; j++, k--) {
            int t = list[j];
            list[j] = list[k];
            list[k] = t;
        }
        return list;
    }
}
